<?php
class UsersController extends AppController {
  //public $scaffold;
  public $helpers = array('Html', 'TWSForm');
  public $components = array('Session', 'Util');
  public $name = 'Users';
  public $paginate;
  public $activeOptions= array('Y' => 'Yes', 'N' => 'No');
  public $verificationOptions= array('Y' => 'Yes', 'N' => 'No');

  function __construct($request=null, $response=null) {
    parent::__construct($request, $response);
    $this->paginate = array(
        'limit' => Configure::read('App.pageResultLimit'),
        'order' => array(
            'User.last_name' => 'asc', 'User.first_name' => 'asc'
            ));
  }

  function admin_index(){
    $this->layout ='admin';
    $this->set('title_for_layout','Users');
    $this->set('users', $this->paginate('User'));
  }

  function admin_edit($id=null) {
    $errorOccurred = false;
    $this->layout = 'admin';
    $this->set('title_for_layout', 'Edit User');
    $this->set('activeOptions', $this->activeOptions);
    $this->set('verificationOptions', $this->verificationOptions);
    //getting roles
    $this->loadModel('Role');
    $roles = $this->Role->query('Select Role.role_name from roles as Role order by Role.role_name ASC');
    $this->set('roles', $roles);
    $this->User->id = $id;
    if ($this->request->is('get')) {
      $this->request->data = $this->User->read();
    } else {
      //check for delete announcement
      if (isset($this->request->data['deleteBtn'])) {
        $this->admin_delete($id);
      } else {

        ////////////////////

        $data = $this->_processRequestData($this->request);
        $this->User->set($data);
        //if ($this->User->validates(array('first_name','last_name'))) { //TODO validation
        //getting roles
        if (empty($data['User']['first_name']) || empty($data['User']['last_name'])) {
          $errorOccurred = true;
          $this->Session->setFlash('First and Last name is required.');
        } else {
          $this->loadModel('Role');
          $roles = $this->Role->query('Select Role.role_name from roles as Role order by Role.role_name ASC');
          $userRoles = array();
          for ($i = 0; $i < sizeof($roles); $i++) {
            if (isset($this->request->data['role' . $i])) {
              array_push($userRoles, $this->request->data['role' . $i]);
            }
          }
          if (empty($userRoles)) {
            $this->Session->setFlash('Please select at least one role for the user.');
            $errorOccurred = true;
          } else {
            if ($this->User->save($data, false, array('first_name', 'last_name', 'active', 'verified'))) {
              //Saving Roles
              $this->loadModel('UserRoles');
              //deleting existing roles
              $result = $this->UserRoles->deleteAll(array('UserRoles.user_id' => $id), false);
              //inserting new entries
              foreach ($userRoles as $userRole) {
                $this->UserRoles->create();
                $this->UserRoles->save(array('UserRoles' => array('user_id' => $id, 'role_name' => $userRole)));
              }
              $this->Session->setFlash('The record was successfully updated.');
              $this->redirect(array('action' => 'index'));
            } else {
              $this->Session->setFlash('An error occurred and the record was not successfully updated.');
              $errorOccurred = true;
            }
          }
        }
        if ($errorOccurred) {
          $this->User->id = $id;
          $dataModified = $this->User->read();
          $dataModified['User']['first_name'] = $data['User']['first_name'];
          $dataModified['User']['last_name'] = $data['User']['last_name'];
          $dataModified['User']['verified'] = $data['User']['verified'];
          $dataModified['User']['active'] = $data['User']['active'];
          $this->request->data = $dataModified;
        }

        $this->render('admin_edit');
        ///////////////////
      }
    }
  }

  //admin
  function admin_delete($id) {
    if ($this->User->delete($id)) {
      $this->Session->setFlash('The record has been deleted.');
      $this->redirect(array('action' => 'index'));
    }
  }

}
?>
